package com.example.healkep;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class IndexgetActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indexget);
        TextView icf = findViewById(R.id.gicf);
        TextView ecf = findViewById(R.id.gecf);
        TextView muscle = findViewById(R.id.gmuscle);
        TextView protein = findViewById(R.id.gprotein);
        TextView ffweight = findViewById(R.id.gffweight);
        TextView perfat = findViewById(R.id.gperfat);
        TextView brate = findViewById(R.id.gbrate);
        Button get = findViewById(R.id.getindex);
        Button tochange = findViewById(R.id.tochangeindex);

        get.setOnClickListener(v->{
            String json = "{\n" +
                    "    \"id\":\"" + 123456 + "\"\n" +
                    "}";
            new Thread(()->{
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(LoginActivity.url + "/index/search")
                            .addHeader("Cookie", LoginActivity.getSessionID())
                            .post(RequestBody.create(MediaType.parse("application/json"), json))
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = Objects.requireNonNull(response.body()).string();
                    JSONObject jsonobject = new JSONObject(responseData);
                    String sicf = jsonobject.getString("icf");
                    icf.setText(sicf);
                    String secf = jsonobject.getString("ecf");
                    ecf.setText(secf);
                    String smuscle = jsonobject.getString("muscle");
                    muscle.setText(smuscle);
                    String sprotein = jsonobject.getString("protein");
                    protein.setText(sprotein);
                    String sffweight = jsonobject.getString("ffweight");
                    ffweight.setText(sffweight);
                    String sperfat = jsonobject.getString("perfat");
                    perfat.setText(sperfat);
                    String sbrate = jsonobject.getString("brate");
                    brate.setText(sbrate);
                }catch (Exception e){
                    runOnUiThread(() -> Toast.makeText(IndexgetActivity.this, "连接异常", Toast.LENGTH_SHORT).show());
                }
            }).start();
        });

        tochange.setOnClickListener(v->{
            Intent intent = new Intent(IndexgetActivity.this,IndexchangeActivity.class);
            startActivity(intent);
        });
    }
}